package com.unicom.mec.device.config;

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.util.ClientBuilder;
import io.kubernetes.client.util.KubeConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * kubernetes交互配置类
 *
 * @author 刘亮武
 * @date 2020年07月02日
 */
@Slf4j
@Configuration
public class KubernetesConfig {

    @Bean
    public CoreV1Api getCoreApi() {
        ApiClient client = null;
        try {
            File file = ResourceUtils.getFile("classpath:config.yml");
            client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(file))).build();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("kubernetes config is not found");
        }
        return new CoreV1Api(client);
    }
}
